﻿using UnityEngine;
using System.Collections;

public class MoveBitch : MonoBehaviour {

	public Cannon_Script cannon;
	public GameObject guy;
	public Vector3 moveSpeedToCoffee;
	public Vector3 moveSpeedToShooting;

	public float fireRateDesacceleration = 0.05f;
	public float fireRateHigh = 0.05f;
	public float fireRateNormal = 0.05f;
	public float fireRateOverloading = 0.05f;

	public float restTime = 1.0f;
	float currentRestTime;

	public float highTime = 5.0f;
	float currentHighTime;
	bool ultimate = false;

	public KeyCode key;

	public GameObject Zzz;
	float ZzzSpawnRate = 1.0f;
	float currentZzzSpawnRate = 0.0f;

	public float coffeeRecoverRate = 0.1f;

	enum guyState {
		Shooting,
		DrinkingCoffee,
		MovingToCoffee,
		MovingToShoot,
	};
	guyState state;

	enum CoffeeState {
		Overdosing,
		Normal,
		High,
	};
	CoffeeState coffeeState;
	float coffeePercentage = 0.0f;

	Vector3 velocity;

	public bool selected = false;
	public SpriteRenderer selectedSprite;

	public GameObject aim;

	// Use this for initialization
	void Start () {
		state = guyState.Shooting;
		coffeeState = CoffeeState.Normal;
		velocity = new Vector3 (0, 0, 0);
	}
	
	// Update is called once per frame
	void Update () {

		switch (state) {
		case guyState.Shooting:
			//cannon.TryToShoot(fireRateDesacceleration);
			if(currentHighTime > 0.0f) {
				//if( Input.GetMouseButton(0) )
				//{
					cannon.ShootAtFireRate(fireRateHigh, ultimate);
					setUltimate(false);
				//}
				currentHighTime -= Time.deltaTime;
				if(currentHighTime < 0.0f) {
					coffeeState = CoffeeState.Normal;
				}
			}
			else
			{
				if (coffeeState == CoffeeState.Normal) {
					//if( Input.GetMouseButton(0) )
					//{
						cannon.ShootAtFireRate(fireRateNormal, ultimate);
						setUltimate(false);
					//}
				}
				//else if( coffeeState == CoffeeState.High ) {
				//	cannon.ShootAtFireRate(fireRateHigh);
				//}
				else if( coffeeState == CoffeeState.Overdosing ) {
					//if( Input.GetMouseButton(0) )
					//{
						cannon.ShootAtFireRate(fireRateOverloading, ultimate);
						setUltimate(false);
					//}
					if(currentZzzSpawnRate < 0.0f)
					{
					   	currentZzzSpawnRate = ZzzSpawnRate;
						Instantiate (Zzz, guy.transform.position, new Quaternion());
					}
					else
					{
						currentZzzSpawnRate -= Time.deltaTime;
					}
				}
			}
			break;
		case guyState.DrinkingCoffee:
			currentRestTime -= Time.deltaTime;
			if (currentRestTime < 0) {
				state = guyState.MovingToShoot;
				velocity = moveSpeedToShooting;
				coffeeState = CoffeeState.High;
				currentHighTime = highTime;
				setUltimate(true);
				coffeePercentage += 0.6f;
			}
			break;
		case guyState.MovingToCoffee:
			guy.transform.Translate( velocity * Time.deltaTime);
			if(guy.transform.localPosition.x <= -0.9f){
				velocity.x = 0.0f;
				state = guyState.DrinkingCoffee;
				currentRestTime = restTime;
			}
			break;
		case guyState.MovingToShoot:
			guy.transform.Translate( velocity * Time.deltaTime);
			if(guy.transform.localPosition.x >= 0.9f)
			{
				cannon.ResetFireRate();
				velocity.x = 0.0f;
				state = guyState.Shooting;
			}
			break;
		}

		if (currentHighTime > 0.0f) {
			GetComponentInChildren<TextMesh>().text = "HIGH!";
		}
		else
		{
			if (coffeePercentage > 0) {
				if( coffeePercentage > 1.0f){
					coffeeState = CoffeeState.Overdosing;
					GetComponentInChildren<TextMesh>().text = "Hangover...";
				}
				else{
					if( coffeeState == CoffeeState.Overdosing){
						coffeeState = CoffeeState.Normal;
					}
					GetComponentInChildren<TextMesh>().text = Mathf.Floor(coffeePercentage * 100.0f) + "%";
				}

				coffeePercentage -= coffeeRecoverRate * Time.deltaTime;//0.1 per second
			}
		}


		//Active on keys
		if (Input.GetKeyUp (key)) {
			OnMouseUp();
		}

		bool cenas = GameObject.FindGameObjectWithTag("Mouse").GetComponent<FollowMousy>().getState() == FollowMousy.mousyState.Aim;
		if (Input.GetMouseButton(0) && selected && cenas) {
			var pos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 1.0f);
			pos = Camera.main.ScreenToWorldPoint(pos);

			aim.transform.position = pos;
		}
	}

	void OnMouseUp (){
		if (!selected) {
			foreach (var item in GameObject.FindObjectsOfType<MoveBitch>()) {
				item.selected = false;	
				item.selectedSprite.enabled = false;
			}
			selected = true;
			selectedSprite.enabled = true;
		}

		//switch (state) {
		//case guyState.Shooting:
		//	velocity = -moveSpeedToCoffee;
		//	state = guyState.MovingToCoffee;
		//	break;
		//case guyState.DrinkingCoffee:
		//	break;
		//case guyState.MovingToCoffee:
		//	break;
		//case guyState.MovingToShoot:
		//	break;
		//}
	}

	void setUltimate(bool value) {
		ultimate = value;
	}
}
